﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BOSS.Domain.Security
{
    /// <summary>
    /// 权限设置
    /// </summary>
    [Serializable]
    public class Privilege
    {
        /// <summary>
        /// 获取 / 设置 权限名称
        /// </summary>
        public virtual string Name { get; private set; }

        /// <summary>
        /// 获取 / 设置 描述信息
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// 获取 / 设置 许可
        /// </summary>
        public virtual PermissionType Permission { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        protected Privilege()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="permission"></param>
        public Privilege(string name, PermissionType permission)
        {
            this.Name = name;
            this.Description = name;
            this.Permission = permission;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="permission"></param>
        public Privilege(string name, string description, PermissionType permission)
        {
            this.Name = name;
            this.Description = description;
            this.Permission = permission;
        }

        /// <summary>
        /// 克隆实例
        /// </summary>
        /// <returns></returns>
        protected internal virtual Privilege Clone()
        {
            return new Privilege(this.Name, this.Permission);
        }

        /// <summary>
        /// 判断是否为指定权限的子权限
        /// </summary>
        /// <param name="privilegeName">待检测权限的名称</param>
        /// <returns></returns>
        public virtual bool IsChildOf(string privilegeName)
        {
            return this.Name.StartsWith(privilegeName + ".", StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// 判断是否为指定权限的父权限
        /// </summary>
        /// <param name="privilegeName">待检测权限的名称</param>
        /// <returns></returns>
        public virtual bool IsParentOf(string privilegeName)
        {
            if (string.IsNullOrEmpty(privilegeName))
            {
                return false;
            }
            return privilegeName.StartsWith(this.Name + ".", StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// 获取父权限
        /// </summary>
        /// <returns></returns>
        public virtual Privilege GetParent()
        {
            int index = this.Name.LastIndexOf(".");
            if (index <= 0)
            {
                return null;
            }
            return new Privilege(this.Name.Substring(0, index), this.Permission);
        }

        /// <summary>
        /// 获取字符串描述
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0} - {1} : {2}", this.Name, this.Description, this.Permission);
        }
    }
}
